import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:mahjong/const/LocationEnum.dart';
import 'package:mahjong/const/MahjongConst.dart';
import 'package:mahjong/const/MahjongSystemConst.dart';
import 'package:mahjong/entity/MessageContext.dart';
import 'package:mahjong/entity/MessagetType.dart';
import 'package:mahjong/util/AudioPlayUtil.dart';
import 'package:mahjong/widget/Bubble.dart';
import 'AbstractMessageHandler.dart';

class MessageInteractive extends AbstractMessageHandler {
  void doMessage(MessageContext message) {
    MessageType result = MessageType.fromJson(message.body);
    if (result.type == MessageTypeConst.sound) {
      showMessage(message.userId, soundMessageArray[int.parse(result.msg)]);
      AudioPlayUtil.playSoundMessage((int.parse(result.msg) + 1).toString());
    } else if (result.type == MessageTypeConst.chart) {
      showMessage(message.userId, result.msg);
    }
  }

  void showMessage(String userId, String value) {
    Positioned _positioned;
    final RenderBox renderBox =
        userLocation[userId].headKey.currentContext.findRenderObject();
    final positionGreen = renderBox.localToGlobal(Offset.zero);
    Text text = Text(
      value,
      style: TextStyle(fontSize: 12.sp),
    );
    switch (userLocation[userId].location) {
      case Location.RIGHT:
        _positioned = Positioned(
            top: positionGreen.dy,
            right: MahjongSystemConst.edgeMargin +
                MahjongSystemConst.userHeaderImageWidth,
            child: SafeArea(
                child: Bubble(
                    color: Colors.white,
                    child: text,
                    direction: BubbleDirection.right)));
        break;
      case Location.TOP:
        print(ScreenUtil().bottomBarHeight);
        _positioned = Positioned(
            top: positionGreen.dy,
            right: ScreenUtil().screenWidth - positionGreen.dx,
            child: Bubble(
                color: Colors.white,
                child: text,
                direction: BubbleDirection.right));
        break;
      case Location.LEFT:
        _positioned = Positioned(
            top: positionGreen.dy,
            left: MahjongSystemConst.edgeMargin +
                MahjongSystemConst.userHeaderImageWidth,
            child: SafeArea(
                child: Bubble(
                    color: Colors.white,
                    child: text,
                    direction: BubbleDirection.left)));
        break;
      case Location.BOTTOM:
        _positioned = Positioned(
            bottom: MahjongSystemConst.bottomHandCardWidth *
                MahjongSystemConst.cardWHScale,
            left: positionGreen.dx + MahjongSystemConst.userHeaderImageWidth,
            child: SafeArea(
                child: Bubble(
                    color: Colors.white,
                    child: text,
                    direction: BubbleDirection.left)));
        break;
      default:
        break;
    }

    BotToast.showText(
      text: "tips",
      onlyOne: false,
      wrapAnimation: (controller, cancelFunc, widget) {
        return Stack(
          children: [_positioned],
        );
      },
    );
  }
}
